package com.client.view;

import java.awt.Color;





import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import com.client.ctrl.RoatCtrl;
import com.entity.Campus;
import com.util.CommonUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;

public class CampusDialog extends JDialog implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private final JPanel contentPanel = new JPanel();
	private JTextField idField;
	private JTextField nameField;
	
	private Campus campus;
	RoatCtrl roatCtrl=new RoatCtrl();
	public CampusDialog(final Campus campus) {
		getContentPane().setBackground(SystemColor.inactiveCaptionBorder);
		this.campus=campus;
	}
	
   
	public void createCampusDialog(final JTable table) {
		
		setIconImage(Toolkit.getDefaultToolkit().getImage("src\\com\\util\\Logal.jpg"));
		setBounds(100, 100, 800, 600);
		setTitle("校区信息");
		getContentPane().setLayout(null);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPanel.setBackground(new Color(245,245,226));
		contentPanel.setBounds(0,0,800,400);
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		
		JLabel campusIdLabel = new JLabel("校区编码:");
		CommonUtil.setFont(campusIdLabel, 5);
		campusIdLabel.setBounds(150, 100, 100, 30);
		contentPanel.add(campusIdLabel);
		
		idField = new JTextField();
		idField.setEditable(false);
		if(campus!=null){
			idField.setText(campus.getCampusno());
		}else{
			idField.setText("cp"+CommonUtil.getRandom(2));
		}
		
		CommonUtil.setFont(idField, 6);
		idField.setBounds(250, 100, 300, 40);
		contentPanel.add(idField);
		idField.setColumns(10);
		
		JLabel campusNamelabel = new JLabel("校区名称:");
		CommonUtil.setFont(campusNamelabel, 5);
		campusNamelabel.setBounds(150, 150, 100, 30);
		contentPanel.add(campusNamelabel);
		
		nameField = new JTextField();
		if(campus!=null){
			nameField.setText(campus.getCampusName());
		}
		CommonUtil.setFont(nameField, 6);
		nameField.setColumns(10);
		nameField.setBounds(250, 150, 300, 40);
		contentPanel.add(nameField);
		
		JLabel campusLoclabel = new JLabel("校区地址:");
		CommonUtil.setFont(campusLoclabel, 5);
		campusLoclabel.setBounds(150, 200, 100, 30);
		contentPanel.add(campusLoclabel);
		
		final JTextArea textArea = new JTextArea();
		CommonUtil.setFont(textArea, 6);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		if(campus!=null){
			textArea.setText(campus.getLoc());
		}
		textArea.setBorder(new LineBorder(Color.GRAY, 1));
		textArea.setBounds(250, 200,300, 120);
		contentPanel.add(textArea);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(245,245,226));
			buttonPane.setBounds(0, 400, 800, 200);
			getContentPane().add(buttonPane);
			buttonPane.setLayout(null);
			{
				final JButton okButton = new JButton("保存");
				CommonUtil.setFont(okButton, 7);
				okButton.setBackground(new Color(142,240,146));
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String campusno=idField.getText();
						String name=nameField.getText();
						String loc=textArea.getText();
						  if( name==null || "".equals(name.trim()) ){
							JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>学校名称不能为空</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
						}else if(loc==null || "".equals(loc)){
							JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>学校地址不能为空</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
						}else{
							roatCtrl.saveCampus(okButton, table,contentPanel, campusno, name, loc);
							setVisible(false);
						}
						
					}
				});
				okButton.setLocation(250,10);
				okButton.setSize(100, 30);
				if(campus==null){
					okButton.setActionCommand("add");
				}else{
					okButton.setActionCommand("update");
				}
				
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			
				JButton cancelButton = new JButton("取消");
				CommonUtil.setFont(cancelButton, 7);
				cancelButton.setBackground(new Color(209,138,249));
				cancelButton.setLocation(400,10);
				cancelButton.setActionCommand("Cancel");
				cancelButton.setSize(100, 30);
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			
		}
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	    setVisible(true);
		
	}


	public void actionPerformed(ActionEvent e) {
		this.setVisible(false);
		
	}
}
